﻿@model Fap.AspNetCore.ViewModel.JqGridViewModel
@{
    Layout = null;
}
<title>
    <fap-multilang lang-key="assess" default-content="绩效考核"></fap-multilang>
</title>

<div class="row">
    <div class="col-xs-12 col-sm-3">
        <div class="widget-box">
            <div class="widget-header widget-header-flat">
                <h4 class="widget-title"><fap-multilang lang-key="scheme_category" default-content="方案分类"></fap-multilang></h4>
                <fap-tree-button id="schemecategory" description="方案分类" tree-id="tree-schemecategory"></fap-tree-button>
            </div>

            <div class="widget-body">
                <div class="widget-main treescrollable">
                    <fap-tree id="schemecategory" is-async="true" get-url="/Assess/Api/SchemeCategory" edit-url="/Assess/Api/SchemeCategory" plugin-dnd="true"></fap-tree>
                </div>
            </div>
        </div>
    </div>
    <div class="col-xs-12 col-sm-9">
        <div class="widget-box">
            <div class="widget-header widget-header-flat">
                <h4 class="widget-title">考核方案</h4>

                <div class="widget-toolbar">
                    <a href="#" data-action="fullscreen" class="orange2">
                        <i class="ace-icon fa fa-expand"></i>
                    </a>
                </div>
                <div class="widget-toolbar">
                    <fap-button id="btnPerfCopy" content="复制方案" btn-tag="link" icon-before="fa fa-clipboard"></fap-button>
                </div>
                <div class="widget-toolbar">
                    <fap-button id="btnStart" content="启动" data-status="Starting" btn-tag="link" icon-before="fa fa-play"></fap-button>
                    <fap-button id="btnArchive" content="归档" data-status="Filed" btn-tag="link" icon-before="fa fa-file-archive-o"></fap-button>
                    <fap-button id="btnClose" content="关闭" data-status="Closed" btn-tag="link" icon-before="fa fa-times"></fap-button>
                </div>
                <div class="widget-toolbar no-border">
                    <fap-button id="btnPerfKpi" content="考核指标" btn-tag="link" icon-before="fa fa-list"></fap-button>
                    <fap-button id="btnPerfObject" content="考核对象" btn-tag="link" icon-before="fa fa-users"></fap-button>
                </div>
            </div>
            <div class="widget-body">
                <div class="widget-main">
                    <fap-grid id="scheme" grid-model="Model" on-form-init-add="afterInitFormCallback" shrink-fit="false" multi-select="true" multi-box-only="true"></fap-grid>
                </div>
            </div>
        </div>
    </div>
</div>


<script>
    var afterInitFormCallback = function () {
        var tree = $('#tree-schemecategory').jstree(true);
        var sel = tree.get_selected();
        if (sel.length) {
            $("#PrmCategory").val(sel[0]).next().val(tree.get_node(sel[0]).text);
        }
    };
    var scripts = [null, null];
    $('.page-content-area').ace_ajax('loadScripts', scripts, function () {
        jQuery(function ($) {
            $(document).one('ajaxloadstart.page', function (e) {
                $('#tree-perfprmcat').jstree('destroy');
            })
              //catUid 主Uid,catName 主名称,childs 子Uid
            var RefreshPrm = function (catUid, catName, childs) {
                //根节点
                if (catUid == "0") {
                    reloadGrid("grid-scheme");
                    return;
                }
                var filter = '{"groupOp":"AND","rules":[{"field":"PrmCategory","op":"eq","data":"' + catUid + '"}]}';
                if (childs != undefined && childs != '') {
                    var selids = childs.concat(catUid);
                    filter = '{"groupOp":"AND","rules":[{"field":"PrmCategory","op":"in","data":"' + selids + '"}]}'
                }
                reloadGrid("grid-scheme", { filters: filter});

            };
            $('#tree-schemecategory').on("changed.jstree", function (e, data) {
                if (data && data.selected && data.selected.length) {
                    var catUid = data.selected[0];
                    var catName = data.node.text;
                    var childs = data.node.children_d;
                    RefreshPrm(catUid, catName, childs);
                }
            });
            //考核指标
            $("#btnPerfKpi").on(ace.click_event, function () {
                var rd = getSelectedRow("grid-scheme");
                if (rd === null) {
                    return;
                }
                bootboxWindow($.lang("kpi", "考核指标"), "/Assess/Manage/KPI", {
                    kpitype: {
                        label: $.lang("kpi_type", "考核指标类型"),
                        className: "btn-primary btn-link",
                        callback: function () {
                            bootboxWindow($.lang("kpi_type", "考核指标类型"), "/Assess/Manage/KPIType"
                                , null, { fid: rd.Fid, schemeName: rd.PrmName });
                            return false;
                        }
                    },
                    scoreModel: {
                        label: $.lang("score_model", "评分方式"),
                        className: "btn-primary btn-link",
                        callback: function () {
                            bootboxWindow($.lang("score_model", "评分方式"), "/Assess/Manage/ScoreModel");
                            return false;
                        }
                    }
                }, { fid: rd.Fid, schemeName: rd.PrmName });
            })
            //考核对象
            $("#btnPerfObject").on(ace.click_event, function () {
                var rd = getSelectedRow("grid-scheme");
                if (rd === null) {
                    return;
                }
                bootboxWindow0($.lang("objectives", "考核对象"), "/Assess/Manage/Objectives", {
                    add: {
                        label: $.lang("add_objectives", "添加考核对象"),
                        className: "btn-primary btn-link",
                        callback: function () {
                            bootboxWindow0($.lang("objectives_selector", "考核对象选择"), "/Assess/Manage/ObjectivesSelector",{
                                selectAll: {
                                    label: $.lang("add_all", "添加当前所有"),
                                    className: "btn-primary btn-link",
                                    callback: function () {
                                        addSelectAll(rd.Fid, rd.ObjectType);
                                        return false;
                                    }
                                },
                                select: {
                                    label: $.lang("add_select", "添加选中项"),
                                    className: "btn-primary btn-link",
                                    callback: function () {
                                        addSelectItem(rd.Fid, rd.ObjectType)
                                        return false;
                                    }
                                }
                            }, { objType: rd.ObjectType });
                            return false;
                        }
                    },
                    examiner: {
                        label: $.lang("set_examiner", "设置考核人"),
                        className: "btn-primary btn-link",
                        callback: function () {
                            var objectives = getObjectives();
                            if (objectives == null) {
                                return false;
                            }
                            bootboxWindow0($.lang("set_examiner", "设置考核人"), "/Assess/Manage/Measure", {
                                add: {
                                    label: $.lang("ok", "确定"),
                                    className: "btn-primary btn-link",
                                    callback: function () {
                                        var objs = $.map(objectives, function (d) {
                                            return d.Fid;
                                        });
                                        addExaminer(objs,rd.Fid);
                                    }
                                }
                            });
                            return false;
                        }
                    }
                }, { fid: rd.Fid});

            })

            $("#btnStart").on(ace.click_event, function () {
                let status = $(this).data("status");
                setSchemeStatus(status);
            })
            $("#btnArchive").on(ace.click_event, function () {
                let status = $(this).data("status");
                setSchemeStatus(status);
            })
            $("#btnClose").on(ace.click_event, function () {
                let status = $(this).data("status");
                setSchemeStatus(status);
            })
            var setSchemeStatus = function (status) {
                var rows = getSelectedRows("grid-scheme");
                if (rows === null) {
                    return;
                }
                let schemeUids = $.map(rows, function (d) {
                    return d.Fid;
                });
                $.post(basePath + "/Assess/Api/SchemeStatus",
                    { schemeUids: schemeUids.join(), status: status }, function (rv) {
                        if (rv.success) {
                            $.msg(rv.msg);
                            refreshGrid("grid-scheme");
                        } else {
                            bootbox.alert(rv.msg);
                        }
                    });
            }

            $("#btnPerfCopy").on(ace.click_event, function () {
                bootbox.confirm("确定要复制一份选中的考核方案?", function (result) {
                    if (result) {
                        var ret = getSelectedRow('grid-scheme');
                        $.post("@Url.Content("~/Assess/Api/CopyScheme/")", { schemeUid: ret.Fid}, function (rv) {
                            if (rv.success) {
                                $.msg(rv.msg);
                                refreshGrid("grid-scheme");
                            } else {
                                bootbox.alert(rv.msg);
                            }
                        })
                    }
                });
            })
            
        })

    })
</script>



